import * as server from "@minecraft/server";
import * as realm_property from "../system/realm_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
const tick_id = new Map();
export function AboveTeleport(block) {
    const entitys = block.dimension.getEntitiesAtBlockLocation(block);
    if (!realm_property.ExpendEnergy(block, -30))
        return;
    for (let index = 0; index < 128; index++) {
        try {
            const target = block.offset({ x: 0, y: index, z: 0 });
            if (target?.typeId !== 'block_gate:portal_above' && target?.typeId !== 'block_gate:portal_below')
                continue;
            entitys.forEach(info => info.teleport(math_tools.Vector.add(target, { x: 0.5, y: 1, z: 0.5 })));
            achieve_plan.PathExecute.Create('魔晶上传-路径显示', 1, {
                location_group: [block.location, target.location],
                particles: ['constant:smoke_rune_purple'],
                offset: math_tools.Vector.CONSTANT_HALF,
                dimension: block.dimension,
                cooldown: 1,
                speed: math_tools.Vector.distance(block, target),
            });
        }
        catch {
            derived_tools.ErrorMessage('<§l§b 魔晶上传 §r>§4 发生错误§r', block, { text: '实体传送失败, 请勿在<§l§m 世界边界 §r>或<§l§n 世界之外 §r>使用!!' });
            break;
        }
    }
}
;
export function BelowTeleport(block) {
    const getEntityGroup = block.dimension.getEntitiesAtBlockLocation(block);
    if (!realm_property.ExpendEnergy(block, -30))
        return;
    for (let index = 0; index < 128; index++) {
        try {
            const target = block.offset({ x: 0, y: -index, z: 0 });
            if (target?.typeId !== 'block_gate:portal_above' && target?.typeId !== 'block_gate:portal_below')
                continue;
            getEntityGroup.forEach(info => info.teleport(math_tools.Vector.add(target, { x: 0.5, y: 1, z: 0.5 })));
            achieve_plan.PathExecute.Create('魔晶下传-路径显示', 1, {
                location_group: [block.location, target.location],
                particles: ['constant:smoke_rune_purple'],
                offset: math_tools.Vector.CONSTANT_HALF,
                dimension: block.dimension,
                cooldown: 1,
                speed: math_tools.Vector.distance(block, target),
            });
        }
        catch {
            derived_tools.ErrorMessage('<§l§b 魔晶下传 §r>§4 发生错误§r', block, { text: '实体传送失败, 请勿在<§l§m 世界边界 §r>或<§l§n 世界之外 §r>使用!!' });
            break;
        }
    }
}
;
export function AboveStacking(object) {
    const block = object.above();
    if (!block?.isAir)
        return;
    block.setPermutation(object.permutation);
}
;
export function VerticalGate(block) {
    const about = block.permutation.getState('STATE:about');
    if (about == 1 || about == 3) {
        const east = block.east();
        const west = block.west();
        if (east?.typeId == block.typeId && east?.permutation.getState('STATE:rune_type') == 0)
            native_tools.TrySetPermutation(east, 'STATE:rune_type', 1);
        if (west?.typeId == block.typeId && west?.permutation.getState('STATE:rune_type') == 0)
            native_tools.TrySetPermutation(west, 'STATE:rune_type', 3);
    }
    else if (about == 2 || about == 4) {
        const north = block.north();
        const south = block.south();
        if (north?.typeId == block.typeId && north?.permutation.getState('STATE:rune_type') == 0)
            native_tools.TrySetPermutation(north, 'STATE:rune_type', 2);
        if (south?.typeId == block.typeId && south?.permutation.getState('STATE:rune_type') == 0)
            native_tools.TrySetPermutation(south, 'STATE:rune_type', 4);
    }
    ;
    const above = block.above();
    const below = block.below();
    if (above?.typeId == block.typeId && above?.permutation.getState('STATE:rune_type') == 0)
        native_tools.TrySetPermutation(above, 'STATE:rune_type', 5);
    if (below?.typeId == block.typeId && below?.permutation.getState('STATE:rune_type') == 0)
        native_tools.TrySetPermutation(below, 'STATE:rune_type', 6);
    const toString = math_tools.Vector.toString(block.location);
    const tick = server.system.runTimeout(() => {
        block.dimension.playSound('close.iron_door', block.location);
        native_tools.TrySetPermutation(block, 'STATE:rune_type', 0);
    }, 100);
    tick_id.set(toString, tick);
}
;
export function PlaneGate(block) {
    const north = block.north();
    const south = block.south();
    const east = block.east();
    const west = block.west();
    if (east?.typeId == block.typeId && east?.permutation.getState('STATE:rune_type') == 0)
        native_tools.TrySetPermutation(east, 'STATE:rune_type', 1);
    if (west?.typeId == block.typeId && west?.permutation.getState('STATE:rune_type') == 0)
        native_tools.TrySetPermutation(west, 'STATE:rune_type', 3);
    if (north?.typeId == block.typeId && north?.permutation.getState('STATE:rune_type') == 0)
        native_tools.TrySetPermutation(north, 'STATE:rune_type', 2);
    if (south?.typeId == block.typeId && south?.permutation.getState('STATE:rune_type') == 0)
        native_tools.TrySetPermutation(south, 'STATE:rune_type', 4);
    const toString = math_tools.Vector.toString(block.location);
    const tick = server.system.runTimeout(() => {
        block.dimension.playSound('close.iron_door', block.location);
        native_tools.TrySetPermutation(block, 'STATE:rune_type', 0);
    }, 100);
    tick_id.set(toString, tick);
}
;
export function Urgent(object) {
    for (let x = -4; x < 4; x++)
        for (let y = -4; y < 4; y++)
            for (let z = -4; z < 4; z++) {
                const target = object.offset({ x, y, z });
                if (target?.typeId != object.typeId)
                    continue;
                const toString = math_tools.Vector.toString(target?.location);
                native_tools.TrySetPermutation(target, 'STATE:rune_type', 0);
                const tick = tick_id.get(toString);
                if (!tick)
                    continue;
                server.system.clearRun(tick);
                tick_id.delete(toString);
            }
}
;
